package com.bmps.springcloud.insuredmicroservice.annotation.accept;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 自定义校验类
 * @author lyw
 * @date 2019-09-10 10:35:21
 */
public class EqualsValidator implements ConstraintValidator<Equals, String> {

    private String targetValue;
    /**
     * 1、初始化方法：通过该方法我们可以拿到我们的注解
     */
    @Override
    public void initialize(Equals constraintAnnotation) {
        //constraintAnnotation.required() 接收我们自定义的属性，是否为空
        targetValue = constraintAnnotation.targetValue();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return StringUtils.equals(value,targetValue);
    }
}
